#include "widget.h"
#include "ui_widget.h"
#include <QShortcut>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //设置窗口的初始大小
    QRect rect = this->geometry();
    this->setGeometry(rect.x(), rect.y(), 500, 500);
    //设置滑动条的范围和初始值
    ui->horizontalSlider->setRange(200, 2000);
    ui->horizontalSlider->setValue(500);
    //设置步长
    ui->horizontalSlider->setSingleStep(50);

    //创建快捷键对象
    QShortcut* shortCut_up = new QShortcut(this);
    QShortcut* shortCut_down = new QShortcut(this);
    //添加快捷键
    shortCut_up->setKey(QKeySequence("="));
    shortCut_down->setKey(QKeySequence("-"));

    //使用信号槽，感知到快捷键被按下执行槽函数
    connect(shortCut_up, &QShortcut::activated, this, &Widget::addValue);
    connect(shortCut_down, &QShortcut::activated, this, &Widget::subValue);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_horizontalSlider_valueChanged(int value)
{
    ui->label->setText(QString("当前值为：") + QString::number(value));
    QRect rect = this->geometry();
    this->setGeometry(rect.x(), rect.y(), value, value);
}

//增加函数
void Widget::addValue()
{
    //获取滑动条的value值
    int value = ui->horizontalSlider->value();
    ui->horizontalSlider->setValue(value + 50);
}
//减少函数
void Widget::subValue()
{
    //获取滑动条的value值
    int value = ui->horizontalSlider->value();
    ui->horizontalSlider->setValue(value - 50);
}
